<template>
  <el-dialog
    v-bind="$attrs"
    :title="$t('flyparam.edit_template_info')"
    v-on="$listeners"
    @open="open"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="200px"
      :model="form"
      @submit.native.prevent
    >
      <el-form-item :label="$t('flyparam.template_mode')">
        <el-radio-group v-model="form.mode" disabled>
          <el-radio :label="1">{{ $t('flyparam.design') }}</el-radio>
          <el-radio :label="2">{{ $t('flyparam.simple') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('flyparam.cfg_format')" prop="fileType">
        <el-radio-group v-model="form.fileType" :disabled="devCount > 0">
          <el-radio :label="1">JSON</el-radio>
          <el-radio :label="2">ASN.1</el-radio>
          <el-radio :label="3">XML</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="Authorization Switch" prop="paramPermissionControl">
        <el-switch
          v-model="form.paramPermissionControl"
          :active-value="1"
          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item :label="$t('common.description')" prop="description">
        <el-input
          v-model="form.description"
          maxlength="255"
          show-word-limit
          type="textarea"
          rows="5"
        />
      </el-form-item> </el-form
    ><template #footer>
      <div>
        <el-button size="small" @click="close">{{
          $t('common.cancel')
        }}</el-button>
        <el-button
          size="small"
          type="primary"
          :loading="loading"
          @click="confirm"
          >{{ $t('common.confirm') }}</el-button
        >
      </div></template
    >
  </el-dialog>
</template>

<script>
import { updateTemplate } from '@/api/parameter'
export default {
  props: {
    info: {
      type: Object
    }
  },
  inject: ['devCount'],
  data() {
    return {
      loading: false,
      form: {
        fileType: this.info.fileType,
        mode: this.info.mode,
        description: this.info.description,
        paramPermissionControl: this.info.paramPermissionControl
      },
      hasData: false, // 该应用是否有绑定终端
      devCount: this.devCount
    }
  },
  created() {
    // this.eventBus.$on('requestedEvent', this.requestedHandler)
  },
  beforeDestroy() {
    // this.eventBus.$off('requestedEvent')
  },
  methods: {
    open() {
      this.$nextTick(() => {
        this.form.fileType = this.info.fileType
        this.form.mode = this.info.mode
        this.form.description = this.info.description
      })
    },
    confirm() {
      this.loading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          updateTemplate(this.$route.params.id, {
            fileType: this.form.fileType,
            description: this.form.description,
            paramPermissionControl: this.form.paramPermissionControl
          })
            .then((res) => {
              this.$message.success(this.$t('common.message.operation_success'))
              this.close()
              this.$emit('confirm')
            })
            .catch((err) => {
              this.$message.error(err.messsage)
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    requestedHandler(hasData) {
      // console.log(hasData)
      // this.requested = true
      // this.hasData = hasData
    },
    close() {
      this.$emit('update:model-value', false)
    }
  }
}
</script>

<style>
</style>
